﻿<script type="text/javascript">
    $(function () {
        getButton("sqlproject");
    });

    function ui_sqlproject_init_list(toolbar) {
        $("#ui_sqlproject_dg").datagrid({       //初始化datagrid
            url: "ashx/rm_sqlproject.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20,
            idField: 'id',
            sortName: 'id',
            sortOrder: 'desc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                { field: 'ck', checkbox: true },
                { field: 'name', title: '项目名称', width: 250, sortable: true,
                    formatter: function (value, row, index) {
                        return value.length > 20 ? '<span>' + value.substring(0, 20) + ' </span> ...' : value;
                    }
                }
            ]],
            columns: [[
                 { field: 'remark', title: '项目内容', width: 600,
                           formatter: function (value, row, index) {
                               return value && $(value).text().length > 50 ? '<span>' + $(value).text().substring(0, 50) + ' </span> ...' : $(value).text();
                           }
                       }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar
            //toolbar: [{
            //    text: '添加',
            //    iconCls: 'icon-add',
            //    handler: function () { ui_sqlproject_add(); }
            //}, {
            //    text: '修改',
            //    iconCls: 'icon-edit',
            //    handler: function () { ui_sqlproject_edit(); }
            //}, '-', {
            //    text: '删除',
            //    iconCls: 'icon-remove',
            //    handler: function () { ui_sqlproject_delete() }
            //}]
        });        
    }
    function ui_sqlproject_add() {
        $("<div/>").dialog({
            id: "ui_sqlproject_add_dialog",
            href: "html/ui_sqlproject_edit.html",
            title: "添加项目",
            iconCls: 'icon-add',
            height: 500,
            width: 800,
            modal: true,
            buttons: [{
                id: "ui_sqlproject_add_btn",
                text: '添 加',
                title: "添加项目",
                iconCls: 'icon-add',
                handler: function () {
                    var name = $('#ui_sqlproject_name').textbox('getValue');
                    if (!name || name.length == 0) {
                        $.show_alert("提示", "请输入项目标题！");
                        return;
                    }
                    if (name.length > 25) {
                        $.show_alert("提示", "项目标题不能超过25个字！");
                        return;
                    }
                    var remark = UE.getEditor('ui_sqlproject_remark_editor').getContent();
                    if (!remark || remark.length == 0) {
                        $.show_alert("提示", "请输入项目内容！");
                        return;
                    }
                    para = {};
                    para.action = 'add';
                    para.timespan = new Date().getTime();
                    para.name = name;                    
                    para.remark = remark;                    
                    $.ajax({
                        url: "ashx/rm_sqlproject.ashx",
                        data: para,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                UE.getEditor('ui_sqlproject_remark_editor').destroy();
                                $("#ui_sqlproject_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "添加成功！");
                                $("#ui_sqlproject_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');

                            } else {
                                $.show_warning("提示", data.msg);
                            }
                        }
                    });                    
                }
            }],
            onLoad: function () {
                var ue = UE.getEditor('ui_sqlproject_remark_editor');
                $("#ui_sqlproject_name").focus();
            },
            onClose: function () {
                UE.getEditor('ui_sqlproject_remark_editor').destroy();
                $("#ui_sqlproject_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_sqlproject_edit() {
        var rows = $("#ui_sqlproject_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的按钮或者双击某个项目");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_sqlproject_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_sqlproject_edit_dialog",
            href: "html/ui_sqlproject_edit.html",
            title: "修改项目",
            iconCls: 'icon-edit',
            height: 500,
            width: 800,
            modal: true,
            buttons: [{
                id: "ui_sqlproject_edit_btn",
                iconCls: 'icon-edit',
                text: '修 改',
                handler: function () {
                    var id = $('#ui_sqlproject_id').val();
                    var name = $('#ui_sqlproject_name').textbox('getValue');
                    if (!name || name.length == 0) {
                        $.show_alert("提示", "请输入项目标题！");
                        return;
                    }
                    if (name.length > 25) {
                        $.show_alert("提示", "项目标题不能超过25个字！");
                        return;
                    }
                    var remark = UE.getEditor('ui_sqlproject_remark_editor').getContent();
                    if (!remark || remark.length == 0) {
                        $.show_alert("提示", "请输入项目内容！");
                        return;
                    }
                    para = {};
                    para.action = 'edit';
                    para.timespan = new Date().getTime();
                    para.name = name;
                    para.remark = remark;
                    para.id = id;
                    $.ajax({
                        url: "ashx/rm_sqlproject.ashx",
                        data: para,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                UE.getEditor('ui_sqlproject_remark_editor').destroy();
                                $("#ui_sqlproject_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "修改成功！");
                                $("#ui_sqlproject_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');

                            } else {
                                $.show_warning("提示", data.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_sqlproject_id").val(row.id);
                $("#ui_sqlproject_name").textbox("setValue", row.name);
                var ue = UE.getEditor('ui_sqlproject_remark_editor');                
                ue.addListener("ready", function () {
                    ue.setContent(row.remark);
                    ue.focus();
                });                             
            },
            onClose: function () {
                UE.getEditor('ui_sqlproject_remark_editor').destroy();
                $("#ui_sqlproject_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_sqlproject_delete() {
        var rows = $("#ui_sqlproject_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的按钮");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的这' + rows.length + '个按钮？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();

                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.ids = ids.join(',');
                $.ajax({
                    url: "ashx/rm_sqlproject.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", "删除成功！");
                            $("#ui_sqlproject_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }
</script>
<div id="ui_sqlproject_layout" class="easyui-layout" data-options="fit:true,border:false">    
    <div data-options="region:'center',border:false">
        <table id="ui_sqlproject_dg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
